/*
 * Viewer.java
 *
 * Created on June 10, 2007, 10:26 PM
 */

package tagcapturer;

import java.awt.Color;
import java.awt.Component;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Vector;

import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

/**
 * 
 * @author Anthony Lam
 */
public class Viewer extends javax.swing.JFrame implements TableModelListener {

	static final boolean DEBUG = true;
	static final String MAIN_VIEW = "Main";

	String selectView;
	String editView;

	Hashtable views = new Hashtable();
	Hashtable dict = new Hashtable();

	TableModel tableModel_Main;

	TableCellRenderer renderer_Main;
	TableCellRenderer renderer;

	// String captureUrl =
	// "http://localhost:8081/epcis-repository-0.2.0/capture";
	String captureUrl;
	private Debugger debuger;

	public Debugger getDebuger() {
		return debuger;
	}

	public void setDebuger(Debugger debuger) {
		this.debuger = debuger;
	}

	/** Creates new form Viewer */
	public Viewer() {
		tableModel_Main = new DefaultTableModel(new Object[][] {},
				new String[] { "Tag ID", "Status", "Time", "Reader",
						"Description", "EPCIS Status" }) {
			Class[] types = new Class[] { java.lang.String.class,
					java.lang.String.class, java.lang.String.class,
					java.lang.String.class, java.lang.String.class,
					java.lang.String.class };
			boolean[] canEdit = new boolean[] { false, false, false, false,
					true, false };

			public Class getColumnClass(int columnIndex) {
				return types[columnIndex];
			}

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		};

		renderer_Main = new CustomTableCellRenderer(MAIN_VIEW);
		renderer = new CustomTableCellRenderer("others");

		tableModel_Main.addTableModelListener(this);

		initComponents();

		setView(MAIN_VIEW);
	}

	public void setCaptureUrl(String inputUrl) {
		this.captureUrl = inputUrl;
	}

	public String getCaptureUrl() {
		return this.captureUrl;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed"
	// desc=" Generated Code ">//GEN-BEGIN:initComponents
	private void initComponents() {
		debuger = new Debugger();
		jDialog_View = new javax.swing.JDialog();
		jLabel1 = new javax.swing.JLabel();
		jTextField_Title = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		jTextField_Readers = new javax.swing.JTextField();
		jLabel3 = new javax.swing.JLabel();
		jTextField_IncludeTags = new javax.swing.JTextField();
		jLabel4 = new javax.swing.JLabel();
		jRadioButton_Current = new javax.swing.JRadioButton();
		jRadioButton_Additions = new javax.swing.JRadioButton();
		jRadioButton_Deletions = new javax.swing.JRadioButton();
		jButton_Cancel = new javax.swing.JButton();
		jButton_Submit = new javax.swing.JButton();
		jLabel_SubmitError = new javax.swing.JLabel();
		buttonGroup_SelectView = new javax.swing.ButtonGroup();
		jDialog_About = new javax.swing.JDialog();
		jLabel5 = new javax.swing.JLabel();
		buttonGroup_ReportSet = new javax.swing.ButtonGroup();
		buttonGroup_EditView = new javax.swing.ButtonGroup();
		buttonGroup_DeleteView = new javax.swing.ButtonGroup();
		jDialog_Epcis = new javax.swing.JDialog();
		jLabel_CaptureUrl = new javax.swing.JLabel();
		jTextField_CaptureUrl = new javax.swing.JTextField();
		jButton_EpcisChange = new javax.swing.JButton();
		jButton_EpcisCancel = new javax.swing.JButton();
		jLabel_EpcisVerifyError = new javax.swing.JLabel();
		jCheckBox_AlarmOnNew = new javax.swing.JCheckBox();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTable_Output = new javax.swing.JTable();
		jButton_ClearList = new javax.swing.JButton();
		jCheckBox_SendToEpcis = new javax.swing.JCheckBox();
		jMenuBar_Main = new javax.swing.JMenuBar();
		jMenu_Menu = new javax.swing.JMenu();
		jMenuItem_AddView = new javax.swing.JMenuItem();
		jMenu_EditView = new javax.swing.JMenu();
		jMenu_SelectView = new javax.swing.JMenu();
		jRadioButtonMenuItem_SelectMainView = new javax.swing.JRadioButtonMenuItem();
		jMenu_DeleteView = new javax.swing.JMenu();
		jSeparator1 = new javax.swing.JSeparator();
		jMenuItem_EpcisConfig = new javax.swing.JMenuItem();
		jMenu_Help = new javax.swing.JMenu();
		jMenuItem_About = new javax.swing.JMenuItem();

		jDialog_View.setModal(true);
		jLabel1.setText("Title");

		jLabel2.setText("Logical Readers");

		jLabel3.setText("Scope of Tag IDs");

		jLabel4.setText("Set of EPCs");

		buttonGroup_ReportSet.add(jRadioButton_Current);
		jRadioButton_Current.setSelected(true);
		jRadioButton_Current.setText("Current");
		jRadioButton_Current.setBorder(javax.swing.BorderFactory
				.createEmptyBorder(0, 0, 0, 0));
		jRadioButton_Current.setMargin(new java.awt.Insets(0, 0, 0, 0));

		buttonGroup_ReportSet.add(jRadioButton_Additions);
		jRadioButton_Additions.setText("Additions");
		jRadioButton_Additions.setBorder(javax.swing.BorderFactory
				.createEmptyBorder(0, 0, 0, 0));
		jRadioButton_Additions.setMargin(new java.awt.Insets(0, 0, 0, 0));

		buttonGroup_ReportSet.add(jRadioButton_Deletions);
		jRadioButton_Deletions.setText("Deletions");
		jRadioButton_Deletions.setBorder(javax.swing.BorderFactory
				.createEmptyBorder(0, 0, 0, 0));
		jRadioButton_Deletions.setMargin(new java.awt.Insets(0, 0, 0, 0));

		jButton_Cancel.setText("Cancel");
		jButton_Cancel.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton_CancelActionPerformed(evt);
			}
		});

		jButton_Submit.setText("Submit the Request");
		jButton_Submit.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton_SubmitActionPerformed(evt);
			}
		});

		jLabel_SubmitError.setForeground(new java.awt.Color(255, 0, 0));
		jLabel_SubmitError.setText(" ");

		org.jdesktop.layout.GroupLayout jDialog_ViewLayout = new org.jdesktop.layout.GroupLayout(
				jDialog_View.getContentPane());
		jDialog_View.getContentPane().setLayout(jDialog_ViewLayout);
		jDialog_ViewLayout
				.setHorizontalGroup(jDialog_ViewLayout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								jDialog_ViewLayout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												jDialog_ViewLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																jLabel_SubmitError,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																388,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(
																jDialog_ViewLayout
																		.createSequentialGroup()
																		.add(
																				jDialog_ViewLayout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.LEADING)
																						.add(
																								org.jdesktop.layout.GroupLayout.TRAILING,
																								jLabel4)
																						.add(
																								org.jdesktop.layout.GroupLayout.TRAILING,
																								jLabel3)
																						.add(
																								org.jdesktop.layout.GroupLayout.TRAILING,
																								jLabel2)
																						.add(
																								org.jdesktop.layout.GroupLayout.TRAILING,
																								jLabel1))
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED)
																		.add(
																				jDialog_ViewLayout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.LEADING)
																						.add(
																								jTextField_Title,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								302,
																								Short.MAX_VALUE)
																						.add(
																								jTextField_Readers,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								302,
																								Short.MAX_VALUE)
																						.add(
																								jTextField_IncludeTags,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								302,
																								Short.MAX_VALUE)
																						.add(
																								jDialog_ViewLayout
																										.createSequentialGroup()
																										.add(
																												jRadioButton_Current)
																										.addPreferredGap(
																												org.jdesktop.layout.LayoutStyle.RELATED)
																										.add(
																												jRadioButton_Additions)
																										.addPreferredGap(
																												org.jdesktop.layout.LayoutStyle.RELATED)
																										.add(
																												jRadioButton_Deletions))))
														.add(
																org.jdesktop.layout.GroupLayout.TRAILING,
																jDialog_ViewLayout
																		.createSequentialGroup()
																		.add(
																				jButton_Submit)
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED)
																		.add(
																				jButton_Cancel)
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED)))
										.addContainerGap()));
		jDialog_ViewLayout
				.setVerticalGroup(jDialog_ViewLayout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								jDialog_ViewLayout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												jDialog_ViewLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel1)
														.add(
																jTextField_Title,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												jDialog_ViewLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel2)
														.add(
																jTextField_Readers,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												jDialog_ViewLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel3)
														.add(
																jTextField_IncludeTags,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												jDialog_ViewLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel4)
														.add(
																jRadioButton_Current)
														.add(
																jRadioButton_Additions)
														.add(
																jRadioButton_Deletions))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												jDialog_ViewLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jButton_Submit)
														.add(jButton_Cancel))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												jLabel_SubmitError,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												14, Short.MAX_VALUE)
										.addContainerGap()));
		jDialog_About.setTitle("CU Tag by CUHK");
		jLabel5.setIcon(new javax.swing.ImageIcon("cu_tag.gif"));

		org.jdesktop.layout.GroupLayout jDialog_AboutLayout = new org.jdesktop.layout.GroupLayout(
				jDialog_About.getContentPane());
		jDialog_About.getContentPane().setLayout(jDialog_AboutLayout);
		jDialog_AboutLayout
				.setHorizontalGroup(jDialog_AboutLayout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								jDialog_AboutLayout
										.createSequentialGroup()
										.addContainerGap()
										.add(jLabel5)
										.addContainerGap(
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		jDialog_AboutLayout
				.setVerticalGroup(jDialog_AboutLayout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								jDialog_AboutLayout
										.createSequentialGroup()
										.addContainerGap()
										.add(jLabel5)
										.addContainerGap(
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		jDialog_Epcis.setName("epcisConfig");
		jLabel_CaptureUrl.setText("Capture Interface URL: ");

		jButton_EpcisChange.setText("Change");
		jButton_EpcisChange
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jButton_EpcisChangeActionPerformed(evt);
					}
				});

		jButton_EpcisCancel.setText("Cancel");
		jButton_EpcisCancel
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jButton_EpcisCancelActionPerformed(evt);
					}
				});

		jLabel_EpcisVerifyError.setForeground(new java.awt.Color(255, 0, 0));

		org.jdesktop.layout.GroupLayout jDialog_EpcisLayout = new org.jdesktop.layout.GroupLayout(
				jDialog_Epcis.getContentPane());
		jDialog_Epcis.getContentPane().setLayout(jDialog_EpcisLayout);
		jDialog_EpcisLayout
				.setHorizontalGroup(jDialog_EpcisLayout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								jDialog_EpcisLayout
										.createSequentialGroup()
										.add(
												jDialog_EpcisLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.TRAILING,
																false)
														.add(
																org.jdesktop.layout.GroupLayout.LEADING,
																jDialog_EpcisLayout
																		.createSequentialGroup()
																		.add(
																				19,
																				19,
																				19)
																		.add(
																				jLabel_EpcisVerifyError,
																				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE))
														.add(
																org.jdesktop.layout.GroupLayout.LEADING,
																jDialog_EpcisLayout
																		.createParallelGroup(
																				org.jdesktop.layout.GroupLayout.TRAILING)
																		.add(
																				jDialog_EpcisLayout
																						.createSequentialGroup()
																						.addContainerGap()
																						.add(
																								jButton_EpcisChange)
																						.add(
																								19,
																								19,
																								19)
																						.add(
																								jButton_EpcisCancel))
																		.add(
																				org.jdesktop.layout.GroupLayout.LEADING,
																				jDialog_EpcisLayout
																						.createSequentialGroup()
																						.add(
																								18,
																								18,
																								18)
																						.add(
																								jLabel_CaptureUrl)
																						.addPreferredGap(
																								org.jdesktop.layout.LayoutStyle.RELATED)
																						.add(
																								jTextField_CaptureUrl,
																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																								267,
																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
										.addContainerGap()));
		jDialog_EpcisLayout
				.setVerticalGroup(jDialog_EpcisLayout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								jDialog_EpcisLayout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												jDialog_EpcisLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel_CaptureUrl)
														.add(
																jTextField_CaptureUrl,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												jDialog_EpcisLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(
																jButton_EpcisCancel,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																23,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(
																jButton_EpcisChange))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED,
												17, Short.MAX_VALUE)
										.add(
												jLabel_EpcisVerifyError,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												14,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)));

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent evt) {
				formWindowClosing(evt);
			}
		});

		jCheckBox_AlarmOnNew.setText("Alarm on New reports");
		jCheckBox_AlarmOnNew.setBorder(javax.swing.BorderFactory
				.createEmptyBorder(0, 0, 0, 0));
		jCheckBox_AlarmOnNew.setEnabled(false);
		jCheckBox_AlarmOnNew.setMargin(new java.awt.Insets(0, 0, 0, 0));
		jCheckBox_AlarmOnNew
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jCheckBox_AlarmOnNewActionPerformed(evt);
					}
				});

		jTable_Output.setModel(tableModel_Main);
		jTable_Output
				.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
		jScrollPane1.setViewportView(jTable_Output);

		jButton_ClearList.setText("Clear List");
		jButton_ClearList
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jButton_ClearListActionPerformed(evt);
					}
				});

		jCheckBox_SendToEpcis.setText("Send to EPCIS");
		jCheckBox_SendToEpcis.setBorder(javax.swing.BorderFactory
				.createEmptyBorder(0, 0, 0, 0));
		jCheckBox_SendToEpcis.setMargin(new java.awt.Insets(0, 0, 0, 0));
		jCheckBox_SendToEpcis
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jCheckBox_SendToEpcisActionPerformed(evt);
					}
				});

		jMenu_Menu.setText("Menu");
		jMenu_Menu.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jMenu_MenuActionPerformed(evt);
			}
		});

		jMenuItem_AddView.setText("Add a View");
		jMenuItem_AddView
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jMenuItem_AddViewActionPerformed(evt);
					}
				});

		jMenu_Menu.add(jMenuItem_AddView);

		jMenu_EditView.setText("Edit a View");
		jMenu_Menu.add(jMenu_EditView);

		jMenu_SelectView.setText("Select a View");
		buttonGroup_SelectView.add(jRadioButtonMenuItem_SelectMainView);
		jRadioButtonMenuItem_SelectMainView.setSelected(true);
		jRadioButtonMenuItem_SelectMainView.setText(MAIN_VIEW);
		jRadioButtonMenuItem_SelectMainView
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jRadioButtonMenuItem_SelectViewActionPerformed(evt);
					}
				});

		jMenu_SelectView.add(jRadioButtonMenuItem_SelectMainView);

		jMenu_Menu.add(jMenu_SelectView);

		jMenu_DeleteView.setText("Delete a View");
		jMenu_Menu.add(jMenu_DeleteView);

		jMenu_Menu.add(jSeparator1);

		jMenuItem_EpcisConfig.setText("EPCIS Configuration");
		jMenuItem_EpcisConfig
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jMenuItem_EpcisConfigActionPerformed(evt);
					}
				});

		jMenu_Menu.add(jMenuItem_EpcisConfig);

		jMenuBar_Main.add(jMenu_Menu);

		jMenu_Help.setText("Help");
		jMenuItem_About.setText("About");
		jMenuItem_About.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jMenuItem_AboutActionPerformed(evt);
			}
		});

		jMenu_Help.add(jMenuItem_About);

		jMenuBar_Main.add(jMenu_Help);

		setJMenuBar(jMenuBar_Main);

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																layout
																		.createSequentialGroup()
																		.add(
																				jCheckBox_AlarmOnNew)
																		.add(
																				25,
																				25,
																				25)
																		.add(
																				jCheckBox_SendToEpcis)
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED,
																				418,
																				Short.MAX_VALUE)
																		.add(
																				jButton_ClearList))
														.add(
																jScrollPane1,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																728,
																Short.MAX_VALUE))
										.addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(
				layout.createSequentialGroup().addContainerGap().add(
						layout.createParallelGroup(
								org.jdesktop.layout.GroupLayout.BASELINE).add(
								jCheckBox_AlarmOnNew).add(jButton_ClearList)
								.add(jCheckBox_SendToEpcis)).addPreferredGap(
						org.jdesktop.layout.LayoutStyle.RELATED).add(
						jScrollPane1,
						org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 377,
						Short.MAX_VALUE).addContainerGap()));
		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void jButton_EpcisCancelActionPerformed(
			java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_EpcisCancelActionPerformed
		jDialog_Epcis.setVisible(false);
	}// GEN-LAST:event_jButton_EpcisCancelActionPerformed

	private void jButton_EpcisChangeActionPerformed(
			java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_EpcisChangeActionPerformed
		String url = jTextField_CaptureUrl.getText();
		// url validation
		if (url.equals("")) {
			jLabel_EpcisVerifyError
					.setText("Capture Interface URL cannot be empty");
			return;
		}
		captureUrl = url;
		jDialog_Epcis.setVisible(false);
	}// GEN-LAST:event_jButton_EpcisChangeActionPerformed

	private void jMenu_MenuActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jMenu_MenuActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_jMenu_MenuActionPerformed

	private void jMenuItem_EpcisConfigActionPerformed(
			java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jMenuItem_EpcisConfigActionPerformed
		jTextField_CaptureUrl.setText(captureUrl);
		jTextField_CaptureUrl.setEnabled(true);
		editView = null;
		jDialog_Epcis.setTitle("EPCIS Configurations");
		jDialog_Epcis.pack();
		jDialog_Epcis.setVisible(true);
	}// GEN-LAST:event_jMenuItem_EpcisConfigActionPerformed

	private void jCheckBox_SendToEpcisActionPerformed(
			java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jCheckBox_SendToEpcisActionPerformed
		View view = (View) views.get(selectView);
		view.setEpcisEnabled(jCheckBox_SendToEpcis.isSelected());
	}// GEN-LAST:event_jCheckBox_SendToEpcisActionPerformed

	private void jButton_ClearListActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_ClearListActionPerformed
		if (selectView.equals(MAIN_VIEW)) {
			((DefaultTableModel) tableModel_Main).setRowCount(0);
		} else {
			View view = (View) views.get(selectView);
			DefaultTableModel defaultTableModel = (DefaultTableModel) view
					.getTableModel();
			defaultTableModel.setRowCount(0);
		}
	}// GEN-LAST:event_jButton_ClearListActionPerformed

	private void formWindowClosing(java.awt.event.WindowEvent evt) {// GEN-FIRST:event_formWindowClosing
		for (Enumeration e = views.keys(); e.hasMoreElements();) {
			String viewName = (String) e.nextElement();
			unregisterView(viewName);
		}
	}// GEN-LAST:event_formWindowClosing

	private void unregisterView(String viewName) {
		try {
			ALESOAPClient.unsubscribe(viewName);
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("exception 2");
		}

		try {
			ALESOAPClient.undefine(viewName);
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("exception 3");
		}
	}

	public synchronized void setView(String viewName) {
		selectView = viewName;
		setTitle("CUHK Tag Capturer - " + viewName);

		for (Enumeration e = buttonGroup_SelectView.getElements(); e
				.hasMoreElements();) {
			JRadioButtonMenuItem jRadioButtonMenuItem = (JRadioButtonMenuItem) e
					.nextElement();
			if (jRadioButtonMenuItem.getText().equals(viewName)) {
				jRadioButtonMenuItem.setSelected(true);
				break;
			}
		}

		try {
			if (selectView.equals(MAIN_VIEW)) {
				jCheckBox_AlarmOnNew.setEnabled(false);
				jCheckBox_AlarmOnNew.setSelected(false);
				jCheckBox_SendToEpcis.setEnabled(false);
				jCheckBox_SendToEpcis.setSelected(false);
				jTable_Output.setDefaultRenderer(Class
						.forName("java.lang.Object"), renderer_Main);
				jTable_Output.setModel(tableModel_Main);

				TableColumn column = null;
				column = jTable_Output.getColumnModel().getColumn(0);
				column.setPreferredWidth(120);
				column = jTable_Output.getColumnModel().getColumn(1);
				column.setPreferredWidth(30);
				column = jTable_Output.getColumnModel().getColumn(2);
				column.setPreferredWidth(100);
				column = jTable_Output.getColumnModel().getColumn(3);
				column.setPreferredWidth(100);
				column = jTable_Output.getColumnModel().getColumn(4);
				column.setPreferredWidth(80);

			} else {
				View view = (View) views.get(viewName);
				jCheckBox_AlarmOnNew.setEnabled(true);
				jCheckBox_AlarmOnNew.setSelected(view.isAlarm());
				jCheckBox_SendToEpcis.setEnabled(true);
				jCheckBox_SendToEpcis.setSelected(view.isEpcisEnabled());
				jTable_Output.setDefaultRenderer(Class
						.forName("java.lang.Object"), renderer);
				jTable_Output.setModel(view.getTableModel());

				TableColumn column = null;
				column = jTable_Output.getColumnModel().getColumn(0);
				column.setPreferredWidth(120);
				column = jTable_Output.getColumnModel().getColumn(1);
				column.setPreferredWidth(30);
				column = jTable_Output.getColumnModel().getColumn(2);
				column.setPreferredWidth(100);
				column = jTable_Output.getColumnModel().getColumn(3);
				column.setPreferredWidth(100);
				column = jTable_Output.getColumnModel().getColumn(4);
				column.setPreferredWidth(80);

				if (jTable_Output.getColumnModel().getColumnCount() == 6) {
					column = jTable_Output.getColumnModel().getColumn(5);
					jTable_Output.removeColumn(column);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("exception 4");
		}

	}

	private void jMenuItem_AboutActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jMenuItem_AboutActionPerformed
		jDialog_About.pack();
		jDialog_About.setVisible(true);
	}// GEN-LAST:event_jMenuItem_AboutActionPerformed

	private void jRadioButtonMenuItem_SelectViewActionPerformed(
			java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jRadioButtonMenuItem_SelectViewActionPerformed
		JRadioButtonMenuItem jRadioButtonMenuItem = (JRadioButtonMenuItem) evt
				.getSource();
		String title = jRadioButtonMenuItem.getText();
		setView(title);

		if (DEBUG) {
			for (Enumeration e = views.keys(); e.hasMoreElements();) {
				String viewName = (String) e.nextElement();
				this.debuger.log("-- View Configuration --");
				this.debuger.log("Selected view: " + viewName);
			}
			System.out.println();
		}
	}// GEN-LAST:event_jRadioButtonMenuItem_SelectViewActionPerformed

	private void jMenuItem_EditViewActionPerformed(
			java.awt.event.ActionEvent evt) {
		JMenuItem jRadioButtonMenuItem = (JMenuItem) evt.getSource();
		String title = jRadioButtonMenuItem.getText();

		View view = (View) views.get(title);
		jTextField_Title.setText(view.getTitle());
		jTextField_Title.setEnabled(false);
		jTextField_Readers.setText(view.getReaders());
		jTextField_IncludeTags.setText(view.getIncludeTags());

		switch (view.getReportSet()) {
		case View.CURRENT:
			jRadioButton_Current.setSelected(true);
			break;
		case View.ADDITIONS:
			jRadioButton_Additions.setSelected(true);
			break;
		case View.DELETIONS:
			jRadioButton_Deletions.setSelected(true);
			break;
		}

		jLabel_SubmitError.setText(null);

		editView = title;

		jDialog_View.setTitle("Edit a View");
		jDialog_View.pack();
		jDialog_View.setVisible(true);
	}

	private void jMenuItem_DeleteViewActionPerformed(
			java.awt.event.ActionEvent evt) {
		JMenuItem jMenuItem_DeleteView = (JMenuItem) evt.getSource();
		String viewName = jMenuItem_DeleteView.getText();

		unregisterView(viewName);

		if (selectView.equals(viewName)) {
			setView(MAIN_VIEW);
		}

		JMenuItem jMenuItem_EditView = null;
		for (Enumeration e = buttonGroup_EditView.getElements(); e
				.hasMoreElements();) {
			JMenuItem jMenuItem = (JMenuItem) e.nextElement();
			if (jMenuItem.getText().equals(viewName)) {
				jMenuItem_EditView = jMenuItem;
				break;
			}
		}

		buttonGroup_EditView.remove(jMenuItem_EditView);
		jMenu_EditView.remove(jMenuItem_EditView);

		JRadioButtonMenuItem jRadioButtonMenuItem_SelectView = null;
		for (Enumeration e = buttonGroup_SelectView.getElements(); e
				.hasMoreElements();) {
			JRadioButtonMenuItem jRadioButtonMenuItem = (JRadioButtonMenuItem) e
					.nextElement();
			if (jRadioButtonMenuItem.getText().equals(viewName)) {
				jRadioButtonMenuItem_SelectView = jRadioButtonMenuItem;
				break;
			}
		}

		buttonGroup_SelectView.remove(jRadioButtonMenuItem_SelectView);
		jMenu_SelectView.remove(jRadioButtonMenuItem_SelectView);

		buttonGroup_DeleteView.remove(jMenuItem_DeleteView);
		jMenu_DeleteView.remove(jMenuItem_DeleteView);

		views.remove(viewName);
	}

	private void jButton_SubmitActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_SubmitActionPerformed
		String title = jTextField_Title.getText();

		// title validation
		if (title.equals("")) {
			jLabel_SubmitError.setText("title cannot be empty. e.g. 'ecspec1'");
			return;
		}

		String readers = jTextField_Readers.getText();

		// reader validation
		if (readers.equals("")) {
			jLabel_SubmitError
					.setText("readers cannot be empty. e.g. 'reader1;reader2'");
			return;
		}

		String includeTags = jTextField_IncludeTags.getText();

		// includeTags validation
		if (includeTags.equals("")) {
			jLabel_SubmitError
					.setText("includeTags cannot be empty. e.g. 'urn:epc:pat:gid-96:20.300.*;urn:epc:pat:gid-96:20.300.1'");
			return;
		}

		int reportSet;

		if (jRadioButton_Current.isSelected()) {
			reportSet = View.CURRENT;
		} else if (jRadioButton_Additions.isSelected()) {
			reportSet = View.ADDITIONS;
		} else {
			reportSet = View.DELETIONS;
		}

		View view = new View();
		view.setTitle(title);
		view.setReaders(readers);
		view.setIncludeTags(includeTags);
		view.setReportSet(reportSet);

		// if edit view, undefine first
		if (editView != null) {
			unregisterView(editView);
		}

		try {
			ALESOAPClient.define(view);
			ALESOAPClient.subscribe(view.getTitle());
		} catch (Exception e) {
			jLabel_SubmitError.setText(e.getMessage());
			e.printStackTrace();
			System.out.println("exception 5");
			return;
		}

		jDialog_View.setVisible(false);

		// if add view
		if (editView == null) {
			views.put(title, view);

			JRadioButtonMenuItem jRadioButtonMenuItem_SelectView = new JRadioButtonMenuItem(
					title);
			jRadioButtonMenuItem_SelectView
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(
								java.awt.event.ActionEvent evt) {
							jRadioButtonMenuItem_SelectViewActionPerformed(evt);
						}
					});

			buttonGroup_SelectView.add(jRadioButtonMenuItem_SelectView);
			jMenu_SelectView.add(jRadioButtonMenuItem_SelectView);

			JMenuItem jMenuItem_EditView = new JMenuItem(title);
			jMenuItem_EditView
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(
								java.awt.event.ActionEvent evt) {
							jMenuItem_EditViewActionPerformed(evt);
						}
					});

			buttonGroup_EditView.add(jMenuItem_EditView);
			jMenu_EditView.add(jMenuItem_EditView);

			JMenuItem jMenuItem_DeleteView = new JMenuItem(title);
			jMenuItem_DeleteView
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(
								java.awt.event.ActionEvent evt) {
							jMenuItem_DeleteViewActionPerformed(evt);
						}
					});

			buttonGroup_DeleteView.add(jMenuItem_DeleteView);
			jMenu_DeleteView.add(jMenuItem_DeleteView);
		} else {
			view = (View) views.get(editView);
			view.setReaders(readers);
			view.setIncludeTags(includeTags);
			view.setReportSet(reportSet);
		}
	}// GEN-LAST:event_jButton_SubmitActionPerformed

	private void jCheckBox_AlarmOnNewActionPerformed(
			java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jCheckBox_AlarmOnNewActionPerformed
		View view = (View) views.get(selectView);
		view.setAlarm(jCheckBox_AlarmOnNew.isSelected());
	}// GEN-LAST:event_jCheckBox_AlarmOnNewActionPerformed

	private void jButton_CancelActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_CancelActionPerformed
		jDialog_View.setVisible(false);
	}// GEN-LAST:event_jButton_CancelActionPerformed

	private void jMenuItem_AddViewActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jMenuItem_AddViewActionPerformed
		jTextField_Title.setText("");
		jTextField_Title.setEnabled(true);
		jTextField_Readers.setText("");
		jTextField_IncludeTags.setText("");
		jRadioButton_Current.setSelected(true);
		jLabel_SubmitError.setText(null);

		editView = null;

		jDialog_View.setTitle("Add a View");
		jDialog_View.pack();
		jDialog_View.setVisible(true);
	}// GEN-LAST:event_jMenuItem_AddViewActionPerformed

	public void tableChanged(TableModelEvent event) {
		if (event.getType() == event.UPDATE && event.getColumn() == 4
				&& selectView.equals(MAIN_VIEW)) {
			int row = event.getFirstRow();
			int column = event.getColumn();
			String tagID = (String) tableModel_Main.getValueAt(row, 0);
			String description = (String) tableModel_Main.getValueAt(row,
					column);
			dict.put(tagID, description);

			for (Enumeration e = views.elements(); e.hasMoreElements();) {
				View view = (View) e.nextElement();

				DefaultTableModel tableModel;
				Vector tagVector;

				tableModel = (DefaultTableModel) view.getTableModel();
				tagVector = (Vector) tableModel.getDataVector();

				for (row = 0; row < tagVector.size(); row++) {
					Vector tag = (Vector) tagVector.elementAt(row);
					if (tagID.equals(tableModel.getValueAt(row, 0))) {
						tableModel.setValueAt(description, row, 4);
					}
				}
			}
		}
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		String cUrl = null;
		final Viewer viewer = new Viewer();
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

			InputStream conf = new FileInputStream("config.properties");
			Properties prop = new Properties();
			prop.load(conf);
			conf.close();
			viewer.getDebuger().log("Configuração:");
			viewer.getDebuger().log(
					"Servidor (Middleware)......... : "
							+ prop.getProperty("server.host"));

			viewer.getDebuger().log("Porta do servidor (Middleware) : "
					+ prop.getProperty("server.port"));
			viewer.getDebuger().log("IP do client.................. : "
					+ prop.getProperty("client.host"));
			viewer.getDebuger().log("Porta do client............... : "
					+ prop.getProperty("client.port"));
			viewer.getDebuger().log("Web Service Openbravo:........ : "
					+ prop.getProperty("openbravo.webservice.base"));

			ALESOAPClient.server_host = prop.getProperty("server.host");
			ALESOAPClient.server_port = prop.getProperty("server.port");
			ALESOAPClient.client_host = prop.getProperty("client.host");
			ALESOAPClient.client_port = prop.getProperty("client.port");

			SocketThrdServer.client_port = Integer.parseInt(prop
					.getProperty("client.port"));
			cUrl = prop.getProperty("epcis.captureurl");

			// configura o webservice do openbravo
			String openbravoWsBase = prop
					.getProperty("openbravo.webservice.base");
			OpenbravoWS.base = openbravoWsBase;

		} catch (Exception e) {
			e.printStackTrace();
		}

		if (cUrl != null && !cUrl.equals("")) {
			viewer.setCaptureUrl(cUrl);
		}

		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				viewer.setVisible(true);
			}
		});

		SocketThrdServer server = new SocketThrdServer(viewer);
		server.listenSocket();
	}

	public TableModel getTableModel_Main() {
		return tableModel_Main;
	}

	public Hashtable getViews() {
		return views;
	}

	public Hashtable getDict() {
		return dict;
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.ButtonGroup buttonGroup_DeleteView;
	private javax.swing.ButtonGroup buttonGroup_EditView;
	private javax.swing.ButtonGroup buttonGroup_ReportSet;
	private javax.swing.ButtonGroup buttonGroup_SelectView;
	private javax.swing.JButton jButton_Cancel;
	private javax.swing.JButton jButton_ClearList;
	private javax.swing.JButton jButton_EpcisCancel;
	private javax.swing.JButton jButton_EpcisChange;
	private javax.swing.JButton jButton_Submit;
	private javax.swing.JCheckBox jCheckBox_AlarmOnNew;
	private javax.swing.JCheckBox jCheckBox_SendToEpcis;
	private javax.swing.JDialog jDialog_About;
	private javax.swing.JDialog jDialog_Epcis;
	private javax.swing.JDialog jDialog_View;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel_CaptureUrl;
	private javax.swing.JLabel jLabel_EpcisVerifyError;
	private javax.swing.JLabel jLabel_SubmitError;
	private javax.swing.JMenuBar jMenuBar_Main;
	private javax.swing.JMenuItem jMenuItem_About;
	private javax.swing.JMenuItem jMenuItem_AddView;
	private javax.swing.JMenuItem jMenuItem_EpcisConfig;
	private javax.swing.JMenu jMenu_DeleteView;
	private javax.swing.JMenu jMenu_EditView;
	private javax.swing.JMenu jMenu_Help;
	private javax.swing.JMenu jMenu_Menu;
	private javax.swing.JMenu jMenu_SelectView;
	private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem_SelectMainView;
	private javax.swing.JRadioButton jRadioButton_Additions;
	private javax.swing.JRadioButton jRadioButton_Current;
	private javax.swing.JRadioButton jRadioButton_Deletions;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JSeparator jSeparator1;
	private javax.swing.JTable jTable_Output;
	private javax.swing.JTextField jTextField_CaptureUrl;
	private javax.swing.JTextField jTextField_IncludeTags;
	private javax.swing.JTextField jTextField_Readers;
	private javax.swing.JTextField jTextField_Title;
	// End of variables declaration//GEN-END:variables

}

class CustomTableCellRenderer extends DefaultTableCellRenderer {

	private String view;

	public CustomTableCellRenderer(String view) {
		this.view = view;
	}

	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {

		Component cell = super.getTableCellRendererComponent(table, value,
				isSelected, hasFocus, row, column);

		if (view.equals(Viewer.MAIN_VIEW)) {
			cell.setForeground(Color.black);
		} else {
			int history = ((Integer) table.getModel().getValueAt(row, 5))
					.intValue();
			if (history == 0) {
				cell.setForeground(Color.red);
			} else if (history < 10) {
				cell.setForeground(Color.black);
			} else {
				cell.setForeground(Color.gray);
			}
		}

		return cell;
	}

}